package pl.edu.mimuw.irs.rcp.controller.editor.report;

import java.io.IOException;
import java.util.Map;

import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.tptp.platform.report.core.provisional.Generator;
import org.eclipse.tptp.platform.report.core.provisional.IChart;
import org.eclipse.tptp.platform.report.core.provisional.RendererException;
import org.eclipse.tptp.platform.report.core.provisional.SWTGraphicViewer;
import org.eclipse.tptp.platform.report.core.provisional.SerializerImpl;
import org.w3c.dom.Document;

@SuppressWarnings("deprecation")
public class Chart {


	public static String LINE = "line";
	public static String AREA = "area";
	public static String STACKED_AREA = "stackedArea";
	public static String SCATTER = "scatter";
	public static String VERTICAL_BAR = "verticalBar";
	public static String VERTICAL_BAR_3D = "verticalBar3D";
	public static String VERTICAL_STACKED_BAR = "verticalStackedBar";
	public static String VERTICAL_STACKED_BAR_3D = "verticalStackedBar3D";
	public static String PIE = "pie";
	public static String PIE_3D = "pie3D";
	public static String METER = "meter";
	
	// *** Attributes

	private SWTGraphicViewer viewer;
	private String title;
	private String type;
	private String xAxisLabel;
	private String yAxisLabel;
	public SWTGraphicViewer getViewer() {
		return viewer;
	}
	public String getTitle() {
		return title;
	}
	public String getType() {
		return type;
	}
	public String getxAxisLabel() {
		return xAxisLabel;
	}
	public String getyAxisLabel() {
		return yAxisLabel;
	}
	
	// *** Constructors

	public Chart(Composite parent, String type, String title, String xAxisLabel, String yAxisLabel) {
		this.title = title;
		this.type = type;
		this.xAxisLabel = xAxisLabel;
		this.yAxisLabel = yAxisLabel;
		// create a new SWT Chart viewer
		viewer = new SWTGraphicViewer(parent, SWT.DIALOG_TRIM
				| SWT.RESIZE);
	}

	// *** Methods

	public void loadValues(Integer[] values) {
		if (values.length == 0) return;
		try {
			Document document = new ChartBuilder(values, getType(), getTitle(), getxAxisLabel(), getyAxisLabel()).createChartDOM();
			IChart chart = SerializerImpl.instance().read(document);
			Generator.instance().run(viewer, chart);
		} catch (IOException e) {
			e.printStackTrace();
		} catch (RendererException e) {
			e.printStackTrace();
		}
	}
	
	public void loadValues(Map<? extends Object, Integer[]> map) {
		if (map == null || map.size() == 0) return;
		try {
			Document document = new ChartBuilder(map, getType(), getTitle(), getxAxisLabel(), getyAxisLabel()).createChartDOM();
			IChart chart = SerializerImpl.instance().read(document);
			Generator.instance().run(viewer, chart);
		} catch (IOException e) {
			e.printStackTrace();
		} catch (RendererException e) {
			e.printStackTrace();
		}
	}
}
